package com.bugreport.trackers.mantis128;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.math.BigInteger;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;

import com.bugreport.utils.BugReportException;

public class Mantis128ConfigurationTestCase {

	private static final Logger logger = Logger.getLogger(Mantis128ConfigurationTestCase.class);

	private Properties properties;

	private Integer idConfig = 1;
	private String categoryName = "General";
	private BigInteger projectId = BigInteger.valueOf(1);
	private String urlPatternMatcher = "www.toto.com";
	private String userLogin = "user";
	private String userPassword = "login";
	private String webServiceUrl = "localhost/mantis128";

	@Before
	public void before() {

		properties = new Properties();
		properties.setProperty(idConfig + Configuration.BUG_TRACKER_KEY, Configuration.KEY);
		properties.setProperty(idConfig + Configuration.URL_PATTERN_MATCHER, urlPatternMatcher);
		properties.setProperty(idConfig + Configuration.CATEGORY_NAME, categoryName);
		properties.setProperty(idConfig + Configuration.PROJECT_ID, projectId.toString());
		properties.setProperty(idConfig + Configuration.USER_LOGIN, userLogin);
		properties.setProperty(idConfig + Configuration.USER_PASSWORD, userPassword);
		properties.setProperty(idConfig + Configuration.WEB_SERVICE_URL, webServiceUrl);
	}

	private void failToLoadConfigurationAndVerifyMissingProperty() {

		try {

			// When
			Configuration.load(properties, idConfig);

			fail("Expected exception");

		} catch (BugReportException ex) {

			// Then
			assertNotNull(ex);
			assertEquals(200, ex.getCode());

			logger.debug(ex.getMessage());
		}
	}

	@Test
	public void loadCompleteConfigurationSuccessfully() throws BugReportException {

		// Given
		assertEquals(7, properties.size());

		// When
		Configuration config = Configuration.load(properties, idConfig);

		// Then
		assertNotNull(config);
		assertNotNull(config.getBugTrackerKey());
		assertNotNull(config.getCategoryName());
		assertNotNull(config.getIdConfig());
		assertNotNull(config.getProjectId());
		assertNotNull(config.getUrlPatternMatcher());
		assertNotNull(config.getUserLogin());
		assertNotNull(config.getUserPassword());
		assertNotNull(config.getWebServiceUrl());

		assertEquals(Configuration.KEY, config.getBugTrackerKey());
		assertEquals(categoryName, config.getCategoryName());
		assertEquals(idConfig, config.getIdConfig());
		assertEquals(projectId, config.getProjectId());
		assertEquals(urlPatternMatcher, config.getUrlPatternMatcher());
		assertEquals(userLogin, config.getUserLogin());
		assertEquals(userPassword, config.getUserPassword());
		assertEquals(webServiceUrl, config.getWebServiceUrl());
	}

	@Test
	public void failToLoadConfigurationMissesCategoryName() {

		// Given
		properties.remove(idConfig + Configuration.CATEGORY_NAME);
		assertEquals(6, properties.size());

		// When
		failToLoadConfigurationAndVerifyMissingProperty();
	}

	@Test
	public void failToLoadConfigurationMissesProjectId() {

		// Given
		properties.remove(idConfig + Configuration.PROJECT_ID);
		assertEquals(6, properties.size());

		// When
		failToLoadConfigurationAndVerifyMissingProperty();
	}

	@Test
	public void failToLoadConfigurationMissesUserLogin() {

		// Given
		properties.remove(idConfig + Configuration.USER_LOGIN);
		assertEquals(6, properties.size());

		// When
		failToLoadConfigurationAndVerifyMissingProperty();
	}

	@Test
	public void failToLoadConfigurationMissesUserPassword() {

		// Given
		properties.remove(idConfig + Configuration.USER_PASSWORD);
		assertEquals(6, properties.size());

		// When
		failToLoadConfigurationAndVerifyMissingProperty();
	}

	@Test
	public void failToLoadConfigurationMissesWebServiceUrl() {

		// Given
		properties.remove(idConfig + Configuration.WEB_SERVICE_URL);
		assertEquals(6, properties.size());

		// When
		failToLoadConfigurationAndVerifyMissingProperty();
	}

	@Test
	public void failToLoadConfigurationMissesBugTrackerKey() {

		// Given
		properties.remove(idConfig + Configuration.BUG_TRACKER_KEY);
		assertEquals(6, properties.size());

		// When
		failToLoadConfigurationAndVerifyMissingProperty();
	}

	@Test
	public void failToLoadConfigurationMissesUrlPatternMatcher() {

		// Given
		properties.remove(idConfig + Configuration.URL_PATTERN_MATCHER);
		assertEquals(6, properties.size());

		// When
		failToLoadConfigurationAndVerifyMissingProperty();
	}

	@Test
	public void failToLoadConfigurationHavingWrongBugTrackerKey() {

		// Given
		properties.setProperty(idConfig + Configuration.BUG_TRACKER_KEY, "somethingelse");
		assertEquals(7, properties.size());

		try {

			// When
			Configuration.load(properties, idConfig);

			fail("Expected exception");

		} catch (BugReportException ex) {

			// Then
			assertNotNull(ex);
			assertEquals(201, ex.getCode());

			logger.debug(ex.getMessage());
		}
	}
}
